package com.lmiot.xyclick.Util;

import android.app.WallpaperInfo;
import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Build;
import android.util.Base64;
import com.lmiot.xyclick.App.LiveWallpaperService;
import com.lmiot.xyclick.App.MyApp;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class DoubleUtils {
    private static final DoubleUtils ourInstance = new DoubleUtils();

    public static DoubleUtils getInstance() {
        return ourInstance;
    }

    private DoubleUtils() {
    }

    public boolean isRunning(Context context, String str) {
        try {
            WallpaperInfo wallpaperInfo = WallpaperManager.getInstance(context).getWallpaperInfo();
            if (wallpaperInfo == null || !wallpaperInfo.getPackageName().equals(str)) {
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public void open(Context context) {
        if (Build.VERSION.SDK_INT >= 34) {
            Intent createChooser = Intent.createChooser(new Intent("android.intent.action.SET_WALLPAPER"), "选择壁纸");
            createChooser.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(createChooser);
            return;
        }
        try {
            Intent intent = new Intent("android.service.wallpaper.CHANGE_LIVE_WALLPAPER");
            intent.putExtra("android.service.wallpaper.extra.LIVE_WALLPAPER_COMPONENT", new ComponentName(context, LiveWallpaperService.class));
            context.startActivity(intent);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void stop() {
        try {
            WallpaperManager.getInstance(MyApp.getContext()).clear();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String bitmapToString(Bitmap bitmap) {
        try {
            Bitmap copy = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            Canvas canvas = new Canvas(copy);
            canvas.drawColor(-1);
            canvas.drawBitmap(bitmap, 0.0f, 0.0f, (Paint) null);
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            copy.compress(Bitmap.CompressFormat.JPEG, 50, byteArrayOutputStream);
            String encodeToString = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0);
            byteArrayOutputStream.close();
            return encodeToString;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public static void setSaveBitmap(Context context, String str) {
        context.getSharedPreferences("DataUtil_1", 0).edit().putString("setSaveBitmap", str).commit();
    }

    public static String getSaveBitmap(Context context) {
        return context.getSharedPreferences("DataUtil_1", 0).getString("setSaveBitmap", "");
    }

    public static Bitmap stringToBitMap(String str) {
        try {
            byte[] decode = Base64.decode(str, 0);
            return BitmapFactory.decodeByteArray(decode, 0, decode.length);
        } catch (Exception unused) {
            return null;
        }
    }
}
